package org.corey.share.misc;

import java.util.Map;

import org.corey.share.db.entity.PhotoAlbumType;
import org.corey.share.db.service.DefaultSercive;
import org.corey.share.db.service.DefaultServiceImpl;

import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

public class PhotoAlbumTypeConvertor extends DefaultTypeConverter{
	
	private DefaultSercive defaultService=new DefaultServiceImpl();
	
	@SuppressWarnings("unchecked")
	public Object convertValue(Map<String, Object> context, Object value,
			Class toType) {
		if(toType==PhotoAlbumType.class){
			String[] valueArray=(String[])value;
			if(valueArray!=null && valueArray.length>0){
				String valueText=valueArray[0];
				Long valueId=Long.parseLong(valueText);
				PhotoAlbumType type=defaultService.getEntity(PhotoAlbumType.class, valueId);
				return type;
			}
		}else{
			if(value!=null){
				PhotoAlbumType type=(PhotoAlbumType)value;
				return type.getId();
			}
		}
		return null;
	}
}
